package org.korifey.ffbdd.api.util;


import org.korifey.ffbdd.api.INode;
import org.korifey.ffbdd.api.INonterminal;
import org.korifey.ffbdd.api.ITraverseStrategy;

public class NodeUtil {

	public static INode _optOne(int var, INode n) {
		if (n.isTerm()) return n;
		INonterminal nt = (INonterminal)n;
		if (nt.var() == var) return nt.one();
		else return n;
	}

	public static INode _optZero(int var, INode n) {
		if (n.isTerm()) return n;
		INonterminal nt = (INonterminal)n;
		if (nt.var() == var) return nt.zero();
		else return n;
	}

	public static int _optVar(INode n) {
		assert n instanceof INonterminal;
		if (n.isTerm()) return Integer.MAX_VALUE;
		INonterminal nt = (INonterminal)n;
		return nt.var();
	}
	
	public static int _minVar(INode n1, INode n2) {
		return Math.min(_optVar(n1), _optVar(n2));
	}

	public static void _iterate(INode n, ITraverseStrategy strategy) {
		//TODO
		/*List<INode> lst = strategy.proccess(n);
		for (INode nn: lst) {
			_iterate(nn, strategy);
		}*/
	}
	
	public static void _mark(INode n) {
		if (n.isMarked()) return;
		n.setMark(true);
		if (n instanceof INonterminal) {
			INonterminal nn = (INonterminal)n;
			_mark(nn.one());
			_mark(nn.zero());
		}
	}
	
	public static void _unmark(INode n) {
		if (!n.isMarked()) return;
		n.setMark(false);
		if (n instanceof INonterminal) {
			INonterminal nn = (INonterminal)n;
			_unmark(nn.one());
			_unmark(nn.zero());
		}
	}

}
